<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Axios案例</title>
</head>
<body>
<div id="app">
    <h2>学生列表</h2>
    <ul>
        <li v-for="student in this.students" :key="student.id">
            {{ student.name }} - {{ student.age }} - {{ student.gender }}
        </li>
    </ul>

    <h2>搜索学生</h2>
    <input type="text" v-model="searchKeyword" placeholder="输入学生姓名">
    <button @click="searchStudents">搜索</button>

    <h2>用户登录</h2>
    <form @submit.prevent="login">
        <input type="text" v-model="username" placeholder="用户名">
        <input type="password" v-model="password" placeholder="密码">
        <button type="submit">登录</button>
    </form>
    <p v-if="loginError">{{ loginError }}</p>
</div>
<script src="js/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            students: [],
            searchKeyword: '',
            username: '',
            password: '',
            loginError: ''
        },
        methods: {
            searchStudents() {
                axios.get("http://localhost:8080/findStudentsByName", {
                    params: {
                        name: this.searchKeyword
                    }
                })
                .then((response) => {
                    this.students = response.data.data;
                })
                .catch((error) => {
                    console.error('There was an error!', error);
                });
            },
            // 用户登录
            login() {
                if (!this.username || !this.password) {
                    this.loginError = "用户名和密码不能为空";
                    return;
                }
                axios.post("http://localhost:8080/login", {
                    username: this.username,
                    password: this.password
                })
                .then((response) => {
                    const data = response.data;
                    if (data.code === 200) {
                        window.location.href = "AxiosDemo.html";
                    } else {
                        this.loginError = data.message;
                    }
                })
                .catch((error) => {
                    console.error('There was an error!', error);
                    this.loginError = "登录失败，请重试";
                });
            },
            // 加载学生列表
            loadStudents() {
                axios.get("http://localhost:8080/findStudents")
                .then((response) => {
                    this.students = response.data.data;
                })
                .catch((error) => {
                    console.error('There was an error!', error);
                });
            }
        },
        mounted() {
            // 调用学生加载方法
            this.loadStudents();
        }
    })
</script>
</body>
</html>